﻿using xCel = Microsoft.Office.Interop.Excel;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Win32;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace MaskData
{
    public partial class mdMain : Form
    {
        // Setup program variables.
        private System.Drawing.Font myFont; // I seriously don't remember why I put this in.
        private string ofn;
        private string version = "1.0RC2"; // Set the program version.
        private string appPath = @"C:\\Program Files\\Mask Data\\error.txt";

        /*********BEGIN REGEX EXPRESSIONS*********/

        // SSN regex expressions. Ex. 313-45-0912 OR 313450912
        // Regex will not match on spaces.
        public string ssnExpressions = @"\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b";
        public string ssnExpressions2 = @"\b[0-9]{3}[0-9]{2}[0-9]{4}\b";

        // Credit Card expressions. Ex. 4456 9801 8877 3212
        public string ccExpressions = @"^[ -]*(?:4[ -]*(?:\d[ -]*){11}(?:(?:\d[ -]*){3})?\d|5[ -]*[1-5](?:[ -]*[0-9]){14}|6[ -]*(?:0[ -]*1[ -]*1|5[ -]*\d[ -]*\d)(?:[ -]*[0-9]){12}|3[ -]*[47](?:[ -]*[0-9]){13}|3[ -]*(?:0[ -]*[0-5]|[68][ -]*[0-9])(?:[ -]*[0-9]){11}|(?:2[ -]*1[ -]*3[ -]*1|1[ -]*8[ -]*0[ -]*0|3[ -]*5(?:[ -]*[0-9]){3})(?:[ -]*[0-9]){11})[ -]*$";

        // Wells Fargo bank accounts. Ex. 0656497001
        public string wfBankAccountExpressions = @"\b[0-9]{10}\b";

        // Bank of America bank accounts. Ex. 20032-30814
        public string baBankAccountExpressions = @"\b[0-9]{5}-[0-9]{5}\b";

        /*********END REGEX EXPRESSIONS*********/



        
        public string Ofn
        {
            get { return ofn; }
            set { ofn = value; }
        }

        public string Version
        {
            get { return version; }
            set { version = value; }
        }

        public System.Drawing.Font MyFont
        {
            get { return myFont; }
            set { myFont = value; }
        }

        public string AppPath
        {
            get { return appPath; }
            set { appPath = value; }
        }

        public CheckBox checkboxPDF
        {
            get { return chkbxPDF; }
            set { chkbxPDF = value; }
        }
                
        public CheckBox checkboxSSN
        {
            get { return chkbxSSN; }
            set { chkbxSSN = value; }
        }

        public CheckBox checkboxCC
        {
            get { return chkbxCC; }
            set { chkbxCC = value; }
        }

        public CheckBox checkboxWellsFargo
        {
            get { return chkbxWellsFargo; }
            set { chkbxWellsFargo = value; }
        }

        public CheckBox checkboxBA
        {
            get { return chkbxBA; }
            set { chkbxBA = value; }
        }

        public CheckBox checkboxText
        {
            get { return chkbxText; }
            set { chkbxText = value; }
        }

        public CheckBox checkboxExcel
        {
            get { return chkbxExcel; }
            set { chkbxExcel = value; }
        }

        public CheckBox checkboxWord
        {
            
            get { return chkbxWord; }
            set { chkbxWord = value; }
        }

        private void mdMain_Load(object sender, EventArgs e)
        {
            // If this is the first time the application is being
            // ran, then create a new registry subkey for the app.
            Registry.CurrentUser.CreateSubKey(@"Software\Mask Data");

            // Set the value of the latest version (to be used later).
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "Version", Version);

            // DEBUG
            //ChkbxText.Visible = false;

            // Check to see if Excel is installed.
            checkExcelInstall();

            // Check to see if all supported types have registry entries 
            // and if the checkboxes are correctly marked.            
            checkDataTypes();
            checkAppSettings();
        }

        private void checkDataTypes()
        {
            // Read what the values are for the checkboxes first and assign them to a string.            
            string _dataSSN = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "SSNHash", "Unchecked"));
            string _dataCC = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "CCHash", "Unchecked"));
            string _dataWells = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "WellsHash", "Unchecked"));
            string _dataBA = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "BAHash", "Unchecked"));

            // Now let's make sure we reflect what they are supposed to be in the GUI.            
            checkboxSSN.Checked = _dataSSN.Equals("Checked");
            checkboxCC.Checked = _dataCC.Equals("Checked");
            checkboxWellsFargo.Checked = _dataWells.Equals("Checked");
            checkboxBA.Checked = _dataBA.Equals("Checked");
        }

        private void checkAppSettings()
        {
            // Read what the values are for the checkboxes first and assign them to a string.
            string _textEnable = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "TextEnableHash", "Unchecked"));
            string _excelEnable = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelEnableHash", "Unchecked"));
            string _wordEnable = Convert.ToString(Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "WordEnableHash", "Unchecked"));

            // Now let's make sure we reflect what they are supposed to be in the GUI.
            checkboxText.Checked = _textEnable.Equals("Checked");
            checkboxExcel.Checked = _excelEnable.Equals("Checked");
            checkboxWord.Checked = _wordEnable.Equals("Checked");
        }

        private void checkExcelInstall()
        {
            // Convert the registry value to a string and assign it to a variable.
            string _installRoot12 = Convert.ToString(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Excel\InstallRoot\", "Path", ""));
            string _installRoot11 = Convert.ToString(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot\", "Path", ""));

            if (_installRoot12 == "")
                if (_installRoot11 == "")
                {
                    // Inform the user of their mistake and dump the program on them.
                    MessageBox.Show("Excel is not installed. Please install Excel and restart the program.",
                        "Mask Data", MessageBoxButtons.OK);

                    // Gracefully exit the application.
                    Application.Exit();
                }
                else
                {
                    return;
                }
        }

        public mdMain()
        {
            InitializeComponent();
        }

        public void OpenFileBox()
        {
            if (chkbxExcel.Checked == true)
            {
                // Instantiate a new instance of the mdSensitiveScanning class.
                Excel _excel = new Excel();

                // Present a file dialog for the user.
                System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
                ofd.Filter = "Excel files | *.xls; *.xlsx";

                // If the user does not press Cancel, then assign the FileName to the Ofn variable.
                if (ofd.ShowDialog() != DialogResult.Cancel)
                {
                    Ofn = ofd.FileName;

                    // Backup the user's file that they open.
                    if (File.Exists(Ofn + ".orig"))
                    {
                        File.Move(Ofn + ".orig", Ofn + ".temp");
                        File.Copy(Ofn, Ofn + ".orig");
                        File.Delete(Ofn + ".temp");
                    }
                    else
                    {
                        File.Copy(Ofn, Ofn + ".orig");
                    }
                }
                else
                {
                    return;
                }

                // Create an instance of our other class with Excel logic so we
                // can have OpenFileBox call the necessary methods.            
                _excel.ExcelOpenSpreadsheets(Ofn);
            }
            else if (chkbxWord.Checked == true)
            {
                // Instantiate a new instance of the mdSensitiveScanning class.
                Word _word = new Word();

                // Present a file dialog for the user.
                System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
                ofd.Filter = "Word files | *.doc; *.docx";

                // If the user does not press Cancel, then assign the FileName to the Ofn variable.
                if (ofd.ShowDialog() != DialogResult.Cancel)
                {
                    Ofn = ofd.FileName;

                    // Backup the user's file that they open.
                    if (File.Exists(Ofn + ".orig"))
                    {
                        File.Move(Ofn + ".orig", Ofn + ".temp");
                        File.Copy(Ofn, Ofn + ".orig");
                        File.Delete(Ofn + ".temp");
                    }
                    else
                    {
                        File.Copy(Ofn, Ofn + ".orig");
                    }

                    // Create an instance of our other class with Word logic so we
                    // can have OpenFileBox call the necessary methods.            
                    _word.WordOpenDocuments(Ofn);
                }
                else
                {
                    return;
                }
            }
            else if (checkboxText.Checked == true)
            {
                // Instantiate a new instance.
                Text _text = new Text();

                // Present a file dialog for the user.
                System.Windows.Forms.OpenFileDialog ofdd = new System.Windows.Forms.OpenFileDialog();
                ofdd.Filter = "Text files | *.txt";

                // If the user does not press Cancel, then assign the FileName to the Ofn variable.
                if (ofdd.ShowDialog() != DialogResult.Cancel)
                {
                    Ofn = ofdd.FileName;

                    // Backup the user's file that they open.
                    if (File.Exists(Ofn + ".orig"))
                    {
                        File.Move(Ofn + ".orig", Ofn + ".temp");
                        File.Copy(Ofn, Ofn + ".orig");
                        File.Delete(Ofn + ".temp");
                    }
                    else
                    {
                        File.Copy(Ofn, Ofn + ".orig");
                    }
                }
                else
                {
                    return;
                }

                // Create an instance of our other class with Text logic so we
                // can have OpenFileBox call the necessary methods.            
                _text.OpenText(Ofn);
            }
            else if (checkboxPDF.Checked == true)
            {
                // Instantiate a new instance PDF doc class.
                PDF _pdf = new PDF();

                // Present a file dialog for the user.
                System.Windows.Forms.OpenFileDialog ofdd = new System.Windows.Forms.OpenFileDialog();
                ofdd.Filter = "PDF files | *.pdf";

                // If the user does not press Cancel, then assign the FileName to the Ofn variable.
                if (ofdd.ShowDialog() != DialogResult.Cancel)
                {
                    Ofn = ofdd.FileName;

                    // Backup the user's file that they open.
                    if (File.Exists(Ofn + ".orig"))
                    {
                        File.Move(Ofn + ".orig", Ofn + ".temp");
                        File.Copy(Ofn, Ofn + ".orig");
                        File.Delete(Ofn + ".temp");
                    }
                    else
                    {
                        File.Copy(Ofn, Ofn + ".orig");
                    }
                }
                else
                {
                    return;
                }

                // Create an instance of our other class with PDF logic so we
                // can have OpenFileBox call the necessary methods.            
                //_pdf.OpenPDF(Ofn);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Exit gracefully.
            System.Windows.Forms.Application.Exit();
        }

        public void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // When clicked, call the function to open a file dialog box
            // and perform some logic.
            OpenFileBox();
        }

        private void aboutMaskDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Our about text for our About box.
            string aboutText =
                "Mask Data\nVersion " + Version + "\nGNU Public License\nAuthor: Woody Hughes <whughes05@gmail.com>";

            string aboutMDVersion = "About Mask Data";

            // Create a Message Box to show our text.
            MessageBox.Show(aboutText, aboutMDVersion, MessageBoxButtons.OK, MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
        }

        private void btnMainApply_Click(object sender, EventArgs e)
        {
            // Set all of the application values.
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "ExcelEnableHash", checkboxExcel.CheckState);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "TextEnableHash", checkboxText.CheckState);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "WordEnableHash", checkboxWord.CheckState);

            // Set all of the data type values.
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "SSNHash", checkboxSSN.CheckState);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "CCHash", checkboxCC.CheckState);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "WellsHash", checkboxWellsFargo.CheckState);
            Registry.SetValue(@"HKEY_CURRENT_USER\Software\Mask Data\", "BAHash", checkboxBA.CheckState);
        }

        private void chkbxExcel_CheckedChanged(object sender, EventArgs e)
        {
            // When the Excel checkbox is checked, we activate the Excel option
            // and turn off the other types.                                    
            if (checkboxExcel.Checked == true)
            {
                checkboxText.Checked = false;
                checkboxWord.Checked = false;
            }
            else
            {
                checkboxExcel.Checked = false;
            }
        }

        private void chkbxText_CheckedChanged(object sender, EventArgs e)
        {
            // When the Text checkbox is checked, we activate the Text option
            // and turn off the other types.            
            if (checkboxText.Checked == true)
            {
                checkboxExcel.Checked = false;
                checkboxWord.Checked = false;
            }
            else
            {
                checkboxText.Checked = false;
            }
        }

        private void chkbxWord_CheckedChanged(object sender, EventArgs e)
        {
            // When the Word checkbox is checked, we activate the Word option
            // and turn off the other types.                                   
            if (checkboxWord.Checked == true)
            {
                checkboxExcel.Checked = false;
                checkboxText.Checked = false;
            }
            else
            {
                checkboxWord.Checked = false;
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            checkboxBA.Checked = true;
            checkboxCC.Checked = true;
            checkboxSSN.Checked = true;
            checkboxWellsFargo.Checked = true;
        }

        private void btnUnselectAll_Click(object sender, EventArgs e)
        {
            checkboxBA.Checked = false;
            checkboxCC.Checked = false;
            checkboxSSN.Checked = false;
            checkboxWellsFargo.Checked = false;
        }
    }
}
